package com.company;

import java.util.Scanner;

/**
 * Created by wb on 16-11-2.
 */
public class MyBank {
    private double allMoney = 0;
    private String strPass = "00112233";
    private int errCount = 0;

    public MyBank(double money) {
        allMoney = money;
    }

    public void init() {
        System.out.println("取款程序启动中.....");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("取款程序启动完毕.");

        Scanner sc = new Scanner(System.in);
        while (true) {
            if (errCount >= 3) {
                System.out.println("取款密码输入错误超过3次");
                return;
            }
            System.out.println("请输入密码:");
            String pwd = sc.nextLine();
            if (!pwd.equals(strPass)) {
                errCount++;
                System.out.println(String.format("输入密码错误请重新输入,可用次数%d", (3 - errCount)));
                continue;
            }
            break;
        }


        while (true) {
            System.out.println(String.format("当前余额%f", allMoney));
            System.out.println("请输入取款金额:");
            double money = 0;
            try {
                money = sc.nextDouble();
                sc.nextLine();
            } catch (Exception e) {
                sc.nextLine();
                System.out.println("取款金额必须为数字");
                continue;
            }
            if (money <= 0) {
                System.out.println("取款金额必须大于0");
                continue;
            }
            if (money > allMoney) {
                System.out.println("余额不足");
                continue;
            }
            allMoney -= money;
            System.out.println(String.format("取款成功，当前余额为%f", allMoney));

            if(allMoney==0)
            {
                System.out.println(String.format("已经没有余额系统将退出"));
                return;
            }

            System.out.println("继续操作请输入y,退出输入n");
            String s = sc.nextLine();
            if (!s.equalsIgnoreCase("y")) {
                return;
            }
        }
    }
}